﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTCustomers : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private DataTable dtProductCategorys = new DataTable();
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 CustomerID = HangSo.DEFAULT_ID;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 30;
        private Int32 totalRecord;
        private String Search = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(EHTWebsiteProject.Administrator.EHTArticle));
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
                    Session["PageIndex"] = pageIndex.ToString();
                }
                catch
                {
                    pageIndex = 1;
                }

                try
                {
                    pageSize = Int32.Parse(Request.QueryString["PageSize"].ToString());
                    Session["PageSize"] = pageSize.ToString();
                }
                catch
                {
                    pageSize = 30;
                }

                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                LoadCustomer();
            }
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbDel.Text = rm.GetString("lbDel", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbCustomer.Text = rm.GetString("lbCustomer", ci);
            lbAddNew.Text = rm.GetString("lbAddNew", ci);
            lbBat.Text = rm.GetString("lbBat", ci);          
            lbTat.Text = rm.GetString("lbTat", ci);
            
        }
        /// <summary>
        /// ham load khach hang
        /// </summary>
        /// 
        [AjaxPro.AjaxMethod]
        public void LoadCustomer()
        {
            KhachHang theKhachhang = new KhachHang();
            theKhachhang.CustomerID = -1;
            theKhachhang.CustomerOrder = "";
            theKhachhang.CustomerVN = "";
            if (IsVN == true)
            {
                rpContactVN.DataSource = KhachHangProvider.GetKhachHangsByFillter(theKhachhang,pageIndex, pageSize, out totalRecord);
                rpContactVN.DataBind();
                plCOntactOther.Visible = false;
            }
            else
            {
                rpContactOther.DataSource = KhachHangProvider.GetKhachHangsByFillter(theKhachhang, pageIndex, pageSize, out totalRecord);
                rpContactOther.DataBind();
                plContactVN.Visible = false;
            }
            this.PaggingControl1.PageIndex = pageIndex;
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PageURL = "EHTCustomers.aspx";
        }


        /// <summary>
        /// Ham check all or uncheck all Other
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        public void CheckAllOther(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
            if (ckAll.Checked)
            {
                foreach (RepeaterItem rp in rpContactOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    ckMenu.Checked = true;
                }

            }
            else
            {


                foreach (RepeaterItem rp in rpContactOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    ckMenu.Checked = false;
                }

            }
        }


        protected void rpContactVN_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                Label lbTrangThai = (Label)e.Item.FindControl("lbTrangThai");
                Image ImagePublish = (Image)e.Item.FindControl("ImagePublish");
                if (lbTrangThai.Text == "True")
                    ImagePublish.ImageUrl = "~/images/tick.png";
                else
                    ImagePublish.ImageUrl = "~/images/Close.png";
            }
            catch { }
        }

        protected void rpContactOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                Label lbTrangThai = (Label)e.Item.FindControl("lbTrangThai");
                Image ImagePublish = (Image)e.Item.FindControl("ImagePublish");
                if (lbTrangThai.Text == "True")
                    ImagePublish.ImageUrl = "~/images/tick.png";
                else
                    ImagePublish.ImageUrl = "~/images/Close.png";
            }
            catch { }
        }

        /// <summary>
        /// Ham check all or uncheck all Other
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        public void CheckAllVN(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
            if (ckAll.Checked)
            {
                foreach (RepeaterItem rp in rpContactVN.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    ckMenu.Checked = true;
                }
            }
            else
            {

                foreach (RepeaterItem rp in rpContactVN.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    ckMenu.Checked = false;
                }
            }
        }
       

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (IsVN == true)
            {
                foreach (RepeaterItem rp in rpContactVN.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpContactOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            for (int i = 0; i < arr.Count; i++)
                KhachHangProvider.DeleteKhachHangByKhachHangID(Int32.Parse(arr[i].ToString()));
            LoadCustomer();
        }

        protected void ImgBat_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (IsVN == true)
            {
                foreach (RepeaterItem rp in rpContactVN.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpContactOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            for (int i = 0; i < arr.Count; i++)
                KhachHangProvider.ChangePublishKhachHangByKhachHangID(Int32.Parse(arr[i].ToString()),true);
            LoadCustomer();
        }

        protected void ImaTat_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (IsVN == true)
            {
                foreach (RepeaterItem rp in rpContactVN.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpContactOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            for (int i = 0; i < arr.Count; i++)
                KhachHangProvider.ChangePublishKhachHangByKhachHangID(Int32.Parse(arr[i].ToString()), false);
            LoadCustomer();
        }
    }
}